<template>
	<view>
		<nav-bar title="选择项目"></nav-bar>
		<view>
			<view class="top-bg"></view>
			<view class="top-data">
				<view class="left">
					<image class="img" :src="transferData.avatar" alt="" />
					<view class="isCheck" v-if="item.isClick && showButton">
						<u-icon name="checkmark-circle-fill" color="rgba(194, 126, 21, 1)" size="15"
							style="background-color: yellow;"></u-icon>
					</view>
				</view>
				<view class="right">
					<view class="title">
						{{ transferData.name }}
						<view class="tag">评分：{{ transferData.score || 0 }} 分</view>
					</view>
					<view class="distance">
						<view>所在门店：{{ transferData.storeName }}</view>
						<view class="name-right">
							<uni-icons type="location-filled" size="20" color="#b4b4b4"></uni-icons>
							<view class="distant">1.1km</view>
							<uni-icons type="right" size="12" color="#676767"></uni-icons>
						</view>
					</view>
					<view class="rate">
						<view class="rate-value">
							<u-rate :count="count" v-model="value" readonly
								active-color="rgba(194, 126, 21, 1)"></u-rate>
							<view style="margin-left: 10rpx;">技师</view>
						</view>
					</view>
					<view class="detail" @click="toMasterDetail(transferData)">
						<text class="synopsis">{{ transferData.synopsis }}</text>
						<text style=" color:#faa040;margin-left: 5rpx;">
							详情
						</text>
					</view>
				</view>
			</view>
		</view>
		<view class="container">
			<view class="title">
				<view class="box-line"></view>
				<view class="text">请选择项目</view>
				<view v-if="isLoading" class="common-loading">
					<image class="loading-img" :src="baseIconUrl + 'icon_loading.png'" />
				</view>
			</view>
			<view class="contain" v-if="rightData && rightData.length > 0">
				<scroll-view :style="{ height: scrollHeight }" scroll-y scroll-with-animation
					class="left menu-scroll-view" :scroll-top="scrollTop" :scroll-into-view="itemId">
					<view v-for="(item, index) in rightData" :key="index" class="left-data lee-tab-item"
						@tap.stop="swichMenu(index)">
						<view class="line" :style="activeIndex === index ? 'background: #C27E15' : ''"></view>
						<view :class="['text', activeIndex === index ? 'active' : 'no-active']">
							{{ item.name }}
						</view>
					</view>
				</scroll-view>
				<scroll-view :style="{ height: scrollHeight, paddingBottom: rightData.length > 0 ? '80rpx' : '' }"
					class="scrollright" :scroll-top="scrollRightTop" scroll-y scroll-with-animation
					@scroll="rightScroll">
					<view class="project-list" v-for="(items, indexs) in rightData" :key="indexs" :id="'item' + index">
						<u-divider :text="items.name" :dashed="true"></u-divider>
						<view class="list" v-for="(item, index) in items.children" :key="index">
							<view class="left">
								<image class="img" :src="item.img" alt="" />
							</view>
							<view class="right">
								<view class="righttitle">{{ item.itemName }}</view>
								<view class="des" @click="toProjectDetail(item)">
									<text class="method">{{ item.method || '' }} </text>
								</view>
								<view class="">
									<text style=" color:#faa040;font-size: 25rpx;">
										详情>>
									</text>
								</view>
								<view class="money-box">
									<view class="red">
										<u-icon name="rmb" color="#fe4d4f" size="15"></u-icon>
										<view class="money">{{ item.price }}</view>
										<view class="rmb-text">原价</view>
									</view>
									<view class="place-order" @click="clickPlaceOrder(item)">去预约</view>
								</view>
							</view>
						</view>
					</view>
					<view class="project-list1" :style="{ height: funMainHeight }"
						style="width: 100%;background-color: #f1f1f1;text-align: center;">
						<u-divider text="已经到底啦~~" :dashed="true"></u-divider>
					</view>
				</scroll-view>
			</view>
			<view v-else class="common-no-data"
				:style="{ height: scrollHeight, paddingBottom: rightData.length > 0 ? '80rpx' : '' }">
				<image class="no-data-img" :src="baseImgUrl + 'img_no_technician.png'" alt="" />
				<view class="text">暂无项目</view>
			</view>
		</view>
		<!-- <u-datetime-picker :show="showDate" v-model="newDate" mode="datetime" :minDate="minDate" :visibleItemCount="9"
			@confirm="confirmDate" @cancel="showDate=false"></u-datetime-picker> -->
	</view>
</template>

<script>
	import NavBar from "@/components/nav-bar"
	import Search from '@/components/search/index'
	import api_technician from '@/api/technician'
	import api_order from '@/api/order'
	import pageConfig from '@/config'

	export default {
		name: "chooseProject",
		components: {
			NavBar,
			Search
		},
		data() {
			return {
				mainCur: 0,
				verticalNavTop: 0,
				list: [{}],
				funMainHeight: '',
				baseImgUrl: pageConfig.baseImgUrl,
				baseIconUrl: pageConfig.baseIconUrl,
				scrollHeight: '',
				newDate: '',
				minDate: new Date().getTime(),
				// showDate: false,
				heightConfig: {},
				transferData: {},
				isLoading: false,
				storeDetail: {},
				chooseData: [],
				show: false,
				count: 5,
				value: 5,
				belongAreaId: '',
				rightData: [],
				receivedData: '',
				mainHeight: '',
				scrollTop: 0, //tab标题的滚动条位置
				oldScrollTop: 0, // tab标题的滚动条旧位置
				activeIndex: 0, // 预设当前项的值
				menuHeight: 0, // 左边菜单的高度
				menuItemHeight: 0, // 左边菜单item的高度
				itemId: '', // 栏目右边scroll-view用于滚动的id
				arr: [], // 储存距离顶部高度的数组
				scrollRightTop: 0, // 右边栏目scroll-view的滚动条高度
				timer: null,
			}
		},
		created() {
			this.heightConfig = this.$store.getters.heightData
			this.scrollHeight = this.heightConfig.mainSearchHeight.split("px")[0] - 20 + "px"
		},
		onLoad(options) {
			// this.mainHeight = this.$store.getters.heightData.noTabMainHeight
			this.transferData = JSON.parse(decodeURIComponent(options.transferData))
			this.getProjectList()
		},
		onShow() {
			this.getMenuItemTop()
		},
		methods: {
			getProjectList() {
				this.isLoading = true
				const params = {
					id: this.transferData.id
				}
				api_technician.fetchProject(params).then(res => {
					this.isLoading = false
					if (res.code !== 200) return
					let result = []
					const data = res.data.itemStoreMap
					for (let key in data) {
						if (data.hasOwnProperty(key)) {
							let item = {
								name: key,
								children: this.processChildren(data[key])
							};
							result.push(item);
						}
					}
					this.rightData = result
					this.storeDetail = res.data.zyStore
					let index = this.rightData[this.rightData.length - 1].children.length
					let mainHeight = Number(this.scrollHeight.split("px")[0])
					this.funMainHeight = parseFloat(mainHeight - parseInt(index * 135) - 18) + 'px'
				}).catch(err => {
					this.isLoading = false
				})
			},
			processChildren(childrenData) {
				return Array.isArray(childrenData) ? childrenData.map(child => ({
					...child, // 复制原始子对象
					preferential: child.memberType === '1' ? child.member : this.$methods.numberMul(
						this.$methods.numberMul(child.price, 0.01),
						child.discount !== null ? child.discount : 100
					).toFixed(2)
				})) : [{
					...childrenData, // 如果 childrenData 是单个对象
					preferential: childrenData.memberType === '1' ? childrenData.member : this.$methods.numberMul(
						this.$methods.numberMul(childrenData.price, 0.01),
						childrenData.discount !== null ? childrenData.discount : 100
					).toFixed(2)
				}];
			},
			clickPlaceOrder(item) {
				const {
					provinceString,
					cityString,
					countyString,
					distance
				} = this.storeDetail
				uni.getStorage({
					key: 'chooseTechnician',
					success: function(res) {
						if(res.data && res.data.belongAreaId){
							this.belongAreaId = res.data.belongAreaId
						}
					}
				});
				const transferData = {
					...this.transferData,
					projectId: item.id,
					projectName: item.itemName,
					price: item.price,
					preferential: item.preferential,
					duration: item.duration,
					regionName: this.storeDetail.regionName,
					belongAreaId: this.belongAreaId || '',
					storeAddress: this.storeDetail.detail,
					// reserveDate: this.reserveDate,
					// reserveTime: this.reserveTime.time || this.reserveTime
				}
				const chooseData = [{
					...transferData,
					subscribeStartTime: item.subscribeStartTime || this.transferData.reserveDate + ' ' + this
						.transferData.reserveTime,

				}]
				const params2 = encodeURIComponent(JSON.stringify(transferData))
				const params = encodeURIComponent(JSON.stringify(chooseData))
				uni.navigateTo({
					url: `/subPages/technician/chooseTime?transferData=${params2}&chooseProject=${params}`
				})

			},
			toMasterDetail(item) {
				uni.navigateTo({
					url: `/subPages/technician/technicianDetail?id=${item.id}`,
				})
			},
			//跳转项目详情
			toProjectDetail(item) {
				// console.log(this.transferData,'this.transferData');
				const transferData = {
					...this.transferData,
					projectId: item.id,
					projectName: item.itemName,
					price: item.price,
					memberType: item.memberType,
					duration: item.duration
				}
				const params = encodeURIComponent(JSON.stringify(transferData))
				const itemParams = encodeURIComponent(JSON.stringify(item))
				uni.navigateTo({
					url: `/subPages/home/projectDetail?item=${itemParams}&transferData=${params}`
				})
			},

			// 获取一个目标元素的高度
			getElRect(elClass, dataVal) {
				new Promise((resolve, reject) => {
					const query = uni.createSelectorQuery().in(this);
					query.select('.' + elClass).fields({
						size: true
					}, res => {
						if (!res) {
							setTimeout(() => {
								this.getElRect(elClass);
							}, 10);
							return;
						}
						this[dataVal] = res.height;
						resolve();
					}).exec();
				})
			},
			getMenuItemTop() {
				new Promise(resolve => {
					let selectorQuery = uni.createSelectorQuery().in(this);
					selectorQuery.selectAll('.project-list').boundingClientRect((rects) => {
						if (!rects.length) {
							setTimeout(() => {
								this.getMenuItemTop();
							}, 10);
							return;
						}
						rects.forEach((rect) => {
							this.arr.push(rect.top - 200)
							// console.log(this.arr);
							resolve();
						})
					}).exec()
				})
			},
			// 设置左边菜单的滚动状态
			async leftMenuStatus(index) {
				this.activeIndex = index;
				// 如果为0，意味着尚未初始化
				if (this.menuHeight == 0 || this.menuItemHeight == 0) {
					await this.getElRect('menu-scroll-view', 'menuHeight');
					await this.getElRect('lee-tab-item', 'menuItemHeight');
				}
				// 将菜单活动item垂直居中
				this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
			},
			// 点击左边的栏目切换
			async swichMenu(index) {
				if (this.arr.length == 0) {
					await this.getMenuItemTop();
				}
				if (index == this.activeIndex) return;
				this.scrollRightTop = this.oldScrollTop;
				this.$nextTick(function() {
					this.scrollRightTop = this.arr[index] - 130;
					this.activeIndex = index;
					this.leftMenuStatus(index);
				})
			},
			// 右边菜单滚动  如果不存在height2，意味着数据循环已经到了最后一个，设置左边菜单为最后一项即可
			async rightScroll(e) {
				this.oldScrollTop = e.detail.scrollTop;
				if (this.arr.length == 0) {
					await this.getMenuItemTop();
				}
				if (this.timer) return;
				if (!this.menuHeight) {
					await this.getElRect('menu-scroll-view', 'menuHeight');
				}
				setTimeout(() => { // 节流
					this.timer = null;
					let scrollHeight = e.detail.scrollTop + 150;
					const mainHeight = parseInt(this.scrollHeight.split('px')[0])
					// console.log(scrollHeight, mainHeight, e.detail.scrollHeight);
					if (scrollHeight + mainHeight >= e.detail.scrollHeight) {
						this.leftMenuStatus(this.arr.length - 1);
						return
					}
					for (let i = 0; i < this.arr.length; i++) {
						let height1 = this.arr[i];
						let height2 = this.arr[i + 1];
						if (!height2 || scrollHeight >= height1 && scrollHeight < height2) {
							this.leftMenuStatus(i);
							break;
							return;
						}
					}
				}, 50)
			}

		}
	}
</script>

<style lang="scss" scoped>
	.top-bg {
		width: 100%;
		height: 120rpx;
		opacity: 1;
		background: rgba(194, 126, 21, 1);
	}

	.top-data {
		opacity: 1;
		border-radius: 17rpx;
		background: rgba(255, 255, 255, 1);
		margin: 0 38rpx;
		margin-top: -100rpx;
		padding: 34rpx 30rpx 30rpx 30rpx;
		display: flex;
		justify-content: space-between;

		.left {
			position: relative;
			width: 182rpx;
			height: 198rpx;

			.img {
				width: 182rpx;
				height: 182rpx;
				border-radius: 13.93rpx;
			}

			.show {
				position: absolute;
				bottom: 0;
				left: 21%;
				width: 103rpx;
				height: 37rpx;
				line-height: 37rpx;
				opacity: 1;
				border-radius: 32rpx;
				background: rgba(255, 105, 40, 1);
				font-size: 20rpx;
				font-weight: 400;
				color: rgba(255, 255, 255, 1);
				text-align: center;
			}

			.isCheck {
				width: 30rpx;
				height: 30rpx;
				position: absolute;
				top: 10rpx;
				right: 10rpx;
			}
		}

		.right {
			width: 66%;
			text-align: left;

			.title {
				font-size: 30rpx;
				font-weight: 500;
				color: rgba(51, 51, 51, 1);
				margin-bottom: 12rpx;
				display: flex;

				.tag {
					width: 114rpx;
					height: 43rpx;
					opacity: 1;
					border-radius: 13rpx;
					background: rgba(255, 241, 238, 1);
					text-align: center;
					line-height: 43rpx;
					margin-left: 12rpx;
					font-size: 20rpx;
					font-weight: 400;
					color: rgba(255, 50, 5, 1);
				}
			}

			.distance {
				display: flex;
				justify-content: space-between;
				font-size: 26rpx;
				font-weight: 400;
				color: rgba(0, 0, 0, 1);
				margin-bottom: 12rpx;

				.name-right {
					display: flex;

					.distant {
						font-size: 22rpx;
						font-weight: 400;
						letter-spacing: 0rpx;
						line-height: 31.86rpx;
						color: rgba(102, 102, 102, 1);
						margin: 0 5rpx 0 6rpx;
						transform: translateY(5rpx);
					}
				}
			}

			.rate {
				display: flex;
				justify-content: space-between;
				font-size: 24rpx;
				font-weight: 400;
				color: rgba(194, 126, 21, 1);

				.rate-value {
					display: flex;
				}
			}

			.detail {
				font-size: 26rpx;
				font-weight: 400;
				color: #333333;
				margin-top: 10rpx;

				.synopsis {
					display: -webkit-box;
					/* 将对象作为弹性伸缩盒子模型显示 */
					-webkit-box-orient: vertical;
					/* 设置伸缩盒子的子元素排列方式为垂直方向 */
					overflow: hidden;
					/* 隐藏超出部分 */
					text-overflow: ellipsis;
					/* 显示省略号 */
					-webkit-line-clamp: 2;
					/* 设置最多显示的行数 */
				}
			}

			// .pre-box {
			//   display: flex;
			//   justify-content: flex-end;
			//   margin-top: -15rpx;

			// }

		}
	}


	.contain {
		width: 100%;
		padding-top: 38rpx;
		display: flex;
	}

	.container {
		width: calc(100% - 76rpx);
		margin: 25rpx 38rpx 0 38rpx;
		border-radius: 24rpx 24rpx 0 0;
		background-color: #fff;
		box-sizing: border-box;

		.title {
			position: relative;
			padding-top: 25rpx;

			.box-line {
				position: absolute;
				left: 0;
				top: 25rpx;
				width: 15rpx;
				height: 48rpx;
				opacity: 1;
				background: rgba(194, 126, 21, 1);

			}

			.text {
				margin-left: 45rpx;
				font-size: 38rpx;
				font-weight: 600;
			}

		}

		.left {
			width: 23%;
			height: 100%;
			background: #FFFFFF;
			opacity: 1;

			.left-data {
				width: 100%;
				padding: 38rpx 0;
				display: flex;
				justify-content: space-between;

				.active {
					font-size: 29rpx;
					font-weight: 500;
					color: #333333;
				}

				.no-active {
					font-weight: 400;
					font-size: 26rpx;
					color: #666666;
				}

				.line {
					width: 7rpx;
					height: 30rpx;
					border-radius: 0rpx 0rpx 0rpx 0rpx;
					opacity: 1;
					transform: translateY(6rpx);
					margin-right: -30rpx;
				}

				.text {
					width: calc(100% - 7rpx);
					display: flex;
					justify-content: center;
					position: relative;
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;

					.tag {
						width: 35rpx;
						height: 35rpx;
						background: #FF2828;
						border-radius: 50%;
						opacity: 1;
						position: absolute;
						right: -30rpx;
						top: -15rpx;
						font-size: 19rpx;
						font-weight: 500;
						color: #FFFFFF;
						line-height: 35rpx;
					}
				}
			}
		}

		.scrollright {
			width: 77%;
			background: #f1f1f1;

			.project-list {
				width: 100%;
				margin-left: 20rpx;
				opacity: 1;

				.list {
					width: calc(100% - 20rpx);
					height: 230rpx;
					margin: 25rpx 0;
					padding: 15rpx;
					border-radius: 5px;
					display: flex;
					justify-content: space-between;
					align-items: center;
					border-bottom: 1rpx solid rgba(230, 230, 230, 1);
					background-color: #fff;

					.left {
						width: 170rpx;
						height: 100%;
						position: relative;
						display: flex;
						justify-content: center;
						align-items: center;

						.img {
							width: 160rpx;
							height: 160rpx;
							opacity: 1;
							border-radius: 13.93rpx;
						}
					}

					.right {
						width: calc(100% - 178rpx);
						height: 88%;

						.righttitle {
							font-size: 30rpx;
							font-weight: 500;
							color: rgba(51, 51, 51, 1);
							margin-bottom: 12rpx;
							width: 100%;
							white-space: nowrap;
							overflow: hidden;
							text-overflow: ellipsis;
						}

						.des {
							font-size: 26rpx;
							color: rgba(51, 51, 51, 1);
							margin-bottom: 10rpx;
							width: 100%;

							.method {
								width: 70%;
								white-space: nowrap;
								overflow: hidden;
								text-overflow: ellipsis;
							}
						}


						.money-box {
							display: flex;
							justify-content: space-between;
							margin-top: 6rpx;

							.red {
								display: flex;
								align-items: center;

								.money {
									font-size: 40.11rpx;
									font-weight: 500;
									letter-spacing: -0.28rpx;
									line-height: 47rpx;
									color: rgba(254, 77, 79, 1);
								}

								.rmb-text {
									font-size: 24rpx;
									font-weight: 400;
									color: rgba(254, 77, 79, 1);
									margin-left: 12rpx;
									// transform: translateY(18rpx);
								}
							}

							.grey {
								display: flex;
								align-items: center;
								color: rgba(102, 102, 102, 1);

								.money {
									font-size: 22.79rpx;
									font-weight: 400;
									letter-spacing: -0.2rpx;
									line-height: 32.99rpx;
									transform: translateY(10rpx);
								}

								.rmb-text {
									font-size: 18.63rpx;
									font-weight: 400;
									letter-spacing: 0rpx;
									line-height: 26.98rpx;
									// transform: translateY(14rpx);
									margin-left: 12rpx;
								}
							}

							.place-order {
								width: 92rpx;
								height: 41rpx;
								opacity: 1;
								border-radius: 32rpx;
								line-height: 41rpx;
								background: rgba(194, 126, 21, 1);
								text-align: center;
								font-size: 24rpx;
								font-weight: 400;
								color: #fff;
							}
						}
					}
				}
			}
		}

		.common-no-data {
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: start;
			background-color: #fff;
		}
	}


	.continue {
		width: 144rpx;
		height: 59rpx;
		line-height: 59rpx;
		opacity: 1;
		border-radius: 47rpx;
		background: rgba(253, 240, 220, 1);
		text-align: center;
		font-size: 26rpx;
		font-weight: 400;
		color: rgba(194, 126, 21, 1);
		margin-right: 20rpx;
	}

	.fix-text {
		font-size: 26rpx;
		font-weight: 400;
		letter-spacing: 0rpx;
		line-height: 37.65rpx;
		color: rgba(102, 102, 102, 1);
	}

	.fix-money {
		font-size: 40.11rpx;
		font-weight: 700;
		color: rgba(254, 77, 79, 1);
		display: flex;
		margin-right: 10rpx;

	}

	.submit {
		width: 164rpx;
		height: 76rpx;
		line-height: 76rpx;
		text-align: center;
		opacity: 1;
		border-radius: 40rpx;
		background: rgba(252, 81, 48, 1);
		font-size: 26rpx;
		color: rgba(255, 255, 255, 1);

	}
</style>